// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境

// 云函数入口函数
exports.main = async (event, context) => {
    const db = cloud.database(),
        role = event.role,
        finished = event.finished,
        sid = event.sid;
    const orders = (await db.collection('UOR').where({
        [`${role}ID`]: sid,
        finished: finished
    }).get()).data;
    const res = [];
    for (item of orders) {
        const data = (await db.collection('repairOrder').where({
            _id: item.orderID
        }).limit(1).get()).data[0];
        const rm = (await db.collection('repairman').where({
            _id: item.repairmanID
        }).field({
            _id: false,
            name: true,
            phone_number: true
        }).limit(1).get()).data[0];
        data.rm = rm;
        res.push(data);
    }
    return res;
}